﻿using API_Service.DBHelper;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;

namespace API_Service.BizLogic.BasicData
{
    public class RouteService:BaseLogicService
    {

        public ExecutionResult<dynamic> GetGroupNameList(string vSectionName = null)
        {
            var result = new ExecutionResult<dynamic>();
            var loopup = new Dictionary<string, dynamic>();

            string sql = @"SELECT DISTINCT [group_name],[group_code],[section_name] FROM [dbo].[c_group_config_t] where 1=1 ";
            var dbParams = new DynamicParameters();
            if (!string.IsNullOrEmpty(vSectionName))
            {
                sql = sql + " AND [section_name] LIKE @section_name  order by group_name asc";                
                dbParams.Add("@section_name", string.Format("%{0}%", vSectionName), 
                    DbType.String, ParameterDirection.Input);
            }
           
            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    if (conn.State != ConnectionState.Open)
                    {
                        conn.Open();
                    }
                    var list = conn.Query(sql,dbParams).ToList();

                    result.Datas = list.ToArray();
                    result.Status = true;
                    result.Message = "Query DB group name list success.";
             
                    Logger.WriteDebug(MethodBase.GetCurrentMethod().Name + " " + result.Message);
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "查询工站清单异常";
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }

            }
            return result;
        }
    }
}